package com.easy.neo4j.service.impl;

import com.alibaba.fastjson.JSON;
import com.easy.common.util.Result;
import com.easy.neo4j.entry.Park;
import com.easy.neo4j.repository.*;
import com.easy.neo4j.service.StationService;
import com.easy.neo4j.entry.PowerBike;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: CoolPro
 * @Desc:
 * @create: 2024-03-16 18:09
 **/

@Component
public class StationServiceImpl implements StationService {

    @Resource
    StationRepository stationRepository; // 修改字段名为 busRepository
    @Resource
    ParkRepository parkRepository;
    @Resource
    SubwayRepository subwayRepository;
    @Resource
    BusLineRepository busLineRepository;
    @Resource
    PowerBikeRepository powerBikeRepository;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public String findAllBusStation(String lat, String lng, String lmt) {
//        System.out.println("公交信息------>经度:"+lat+"    纬度:"+lng);
        return JSON.toJSONString(stationRepository.findAllSites(lat, lng, lmt));
    }

    @Override
    public String findAllPark(String lat, String lng) {
        return JSON.toJSONString(parkRepository.findAllPark(lat, lng, "500"));
    }
    //根据城市代码查询共享单车停放点信息


    @Override
    public String findAllSubway(String lat, String lng) {
        return JSON.toJSONString(subwayRepository.findALlSubway(lat, lng));
    }

    @Override
    public String findBusLine(String lineId) {
//        System.out.println("公交车线路ID------>线路ID:"+lineId);
        String regex = "\\{\"\\$ref\":\"\\$\\[\\d+\\]\\\"},";
        String regex2 = ",\\{\"\\$ref\":\"\\$\\[\\d+\\]\\\"}";
        return JSON.toJSONString(busLineRepository.findLine(lineId)).replaceAll(regex, "").replaceAll(regex2, "");
    }

    /**
     * @param cityCode 城市代码
     * @param address     跳过多少行数据
     * @param limit    一共显示多少条
     * @Author: CoolPro
     * @Date: 20/3/2024
     * @Description: 根据城市代码查询共享单车停放点信息(分页查询)
     * @Name: findParkByCityCode
     * @parmas: [cityCode, skip, limit]
     * @return: java.lang.String
     */
    @Override
    public String findParkByCityCode(String address, String cityCode, Integer page, Integer limit) {
        List<Park> allParkByCityCode = parkRepository.findAllParkByCityCode(address, cityCode, (page - 1) * limit, limit);
        for (Park park : allParkByCityCode) {
            List<PowerBike> bike = powerBikeRepository.findBikeByParkName(park.getName());
            park.setCount(bike.size());
            for (PowerBike powerBike : bike) {
                if (Integer.parseInt(powerBike.getPower()) >= 40) {
                    park.setGoodCount(park.getGoodCount() + 1);
                } else if (Integer.parseInt(powerBike.getPower()) < 40 && Integer.parseInt(powerBike.getPower()) > 0) {
                    park.setLowCount(park.getLowCount() + 1);
                } else if (Integer.parseInt(powerBike.getPower()) == 0) {
                    park.setDamageCount(park.getDamageCount() + 1);
                }
                park.setPlatform(powerBike.getBike_plateform(), 1);
            }
            if (bike.size() == 0) {
                park.setPlatform("轻悠", 0);
            }
        }
        return JSON.toJSONString(allParkByCityCode);
    }

    /**
     * @Author: CoolPro
     * @Date: 26/3/2024
     * @Description: 根据城市代码查询所有单车停放点的总数
     * @Name: getParkCountByCityCode
     * @return: java.lang.String
     */

    @Override
    public String getParkCountByCityCode(String address, String cityCode) {
        Map<String, Integer> count = new HashMap<>();
        count.put("count", parkRepository.getParkCount(address, cityCode));
        return JSON.toJSONString(count);
    }

    /**
     * @Author: CoolPro
     * @Date: 26/3/2024
     * @Description: 根据线路名称查询线路中所有站点信息
     * @Name: findSubwayByLineName
     * @return: java.lang.String
     */
    @Override
    public String findSubwayByLineName(String lineName) {
        return JSON.toJSONString(subwayRepository.findSubwayByLineName(lineName + "号线"));
    }

    /**
     * @Author: CoolPro
     * @Date: 26/3/2024
     * @Description: 根据地点名称查询单车信息
     * @Name: findBikeByParkName
     * @param parkName
     * @return: java.lang.String
     */
    @Override
    public String findBikeByParkName(String parkName) {
        return JSON.toJSONString(powerBikeRepository.findBikeByParkName(parkName));
    }

    /**
     * @param parkName 单车停放点名称
     * @param lat      经度
     * @param lng      纬度
     * @Author: CoolPro
     * @Date: 30/3/2024
     * @Description: 修改坐标点位置
     * @Name: updateParkPointByName
     * @return: com.eazy.eazydemo.util.Result<java.lang.String>
     */

    @Override
    public Result<String> updateParkPointByName(String parkName, String lat, String lng) {
        parkRepository.updateParkPointByName(parkName, lat, lng);
        return Result.success();
    }

    @Override
    public Result<String> addPark(String address, String cityCode, String reginId, String lat, String lng, String parkName) {
        parkRepository.addPark(address, cityCode, reginId, lat, lng, parkName);
        return Result.success();
    }

    @Override
    public Result<String> createNearbyRelationships(String address) {
        parkRepository.createNearbyRelationships(address);
        return Result.success();
    }


}
